package
        com.zzc.sale.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hos.entity.stock.Provider;

import com.hos.utils.WebUtil;
import com.hos.vo.Result;
import com.zzc.sale.feigns.UserFeign;
import com.zzc.sale.mapper.ProducerMapper;
import com.zzc.sale.mapper.ProviderMapper;
import com.zzc.sale.service.ProviderService;
import com.zzc.sale.vo.ProviderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * @author：xIAO_ZHOU_TX
 * @date：Created in 2023/12/15 12:12
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class ProviderServiceImpl implements ProviderService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private UserFeign userFeign;
    @Resource
    private ProviderMapper providerMapper;
    @Override
    public Page<Provider> listProviderForPage(ProviderVo providerVo) {
        Page<Provider> page =new Page<>(providerVo.getPageNum(), providerVo.getPageSize());
        LambdaQueryWrapper<Provider> wrapper =new LambdaQueryWrapper<>();
        //供应商名称
        if(StringUtils.hasText(providerVo.getProviderName())){
            wrapper.like(Provider::getProviderName,providerVo.getProviderName());
        }
        //联系人
        if(StringUtils.hasText(providerVo.getContactName())){
            wrapper.like(Provider::getContactName,providerVo.getContactName());
        }
        //供应商电话
        if(StringUtils.hasText(providerVo.getContactTel())){
            wrapper.eq(Provider::getContactTel,providerVo.getContactTel());
        }
        //状态
        if(StringUtils.hasText(providerVo.getStatus())){
            wrapper.eq(Provider::getStatus,providerVo.getStatus());
        }
        //删除状态
        if(Objects.nonNull(providerVo.getDelFlag()) && providerVo.getDelFlag()==0){
            wrapper.eq(Provider::getDelFlag,providerVo.getDelFlag());
        }
        return providerMapper.selectPage(page,wrapper);
    }

    @Override
    public Integer addProvider(Provider provider) {
        //添加创建时间
        provider.setCreateTime(new Date());
        //  获取创建者信息
        String token = WebUtil.getToken();
        String userName = userFeign.selectUserByPhone(redisTemplate.opsForValue().get("user:" + token)).getUserName();
        provider.setCreateBy(userName);
        return providerMapper.insert(provider);
    }

    @Override
    public Integer deleteProviderByIds(List<Long> ids) {
        return providerMapper.deleteBatchIds(ids);
    }

    @Override
    public Integer updateProvider(Provider provider) {
        // 获取修改者信息
        String token = WebUtil.getToken();
        String userName = userFeign.selectUserByPhone(redisTemplate.opsForValue().get("user:" + token)).getUserName();
        provider.setCreateBy(userName);
        //获取修改时间
        provider.setUpdateTime(new Date());
        return providerMapper.updateById(provider);
    }

    @Override
    public Provider getProviderById(Long providerId) {
        return providerMapper.selectById(providerId);
    }

    @Override
    public List<Provider> selectAllProvider() {
        return providerMapper.selectList(null);
    }
}